package com.shujia.MapReduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;

public class Demo11Question2 {
    public static class MyMapper extends Mapper<LongWritable, Text, Text, Text> {

        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context) throws IOException, InterruptedException {
            // 18620192711,15733218050,1506628174,1506628265,650000,810000
            String[] splits = value.toString().split(",");
            if (splits.length == 6) {
                String caller = splits[0];
                String receiver = splits[1];
                String start_time = splits[2];
                String end_time = splits[3];

                // 通话时长
                Long sec = Long.parseLong(end_time) - Long.parseLong(start_time);
                context.write(new Text(caller), new Text("c" + "," + sec));
                context.write(new Text(receiver), new Text("r" + "," + sec));

            }


        }
    }

    public static class MyReducer extends Reducer<Text, Text, Text, Text> {
        @Override
        protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
//            String phoneNum = key.toString();
            int call_time = 0;
            int call_sec = 0;
            int receive_time = 0;
            int receive_sec = 0;
            for (Text value : values) {
                String[] splits = value.toString().split(",");
                String s = splits[0];
                int sec = Integer.parseInt(splits[1]);
                if ("c".equals(s)) {
                    call_time++;
                    call_sec += sec;
                } else if ("r".equals(s)) {
                    receive_time++;
                    receive_sec += sec;
                }
            }
            context.write(key, new Text(call_sec + "," + call_time + "," + receive_sec + "," + receive_time));
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        job.setJobName("Demo11Question2");
        job.setJarByClass(Demo11Question2.class);

        // 配置Map任务
        job.setMapperClass(MyMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setReducerClass(MyReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);


        // 配置输入输出路径
        FileInputFormat.addInputPath(job, new Path("/ques1/data1/"));
        // 判断输出路径是否存在，存在即删除
        FileSystem fs = FileSystem.get(job.getConfiguration());
        if (fs.exists(new Path("/ques2/res"))) {
            fs.delete(new Path("/ques2/res"), true);
        }
        FileOutputFormat.setOutputPath(job, new Path("/ques2/res"));

        job.waitForCompletion(true);
    }
}
